package com.hb.commons;

import com.baomidou.mybatisplus.annotation.TableField;
import com.fasterxml.jackson.annotation.JsonFilter;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import java.io.Serializable;

/**
 * 分页参数实体类
 * 该实体类用于在以表实体接收参数时接收分页相关参数
 * 分页参数中默认页数为1, 默认每页行数为10
 *
 * @author zhaojk
 */
@Getter
@Setter
@ToString
@JsonFilter("entityFilter") // 该注解用于忽略本类中的属性不被序列化
public class PageEntity implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 当前页数
     */
    @TableField(exist = false)
    protected Integer page;

    /**
     * 每页条数
     */
    @TableField(exist = false)
    protected Integer limit;

    /**
     * 偏移量(MySQL用)
     */
    @TableField(exist = false)
    protected Integer offset;

    public Integer getPage() {
        if (page == null) {
            return 1;
        }
        return page;
    }

    public Integer getLimit() {
        if (limit == null) {
            return 10;
        }
        return limit;
    }

    public Integer getOffset() {
        Integer page = this.getPage();
        Integer limit = this.getLimit();
        if (offset == null) {
            offset = (page - 1) * limit;
        }
        return offset;
    }

}
